package xyz.robinbingo.leetcode.a20.b06.c23;

public class Solution {
    public String addBinary(String a, String b) {
        StringBuilder sb = new StringBuilder();
        int aL = a.length();
        int bL = b.length();
        int max = Math.max(aL, bL);
        int rs = 0;
        for (int i = 0; i < max; i++) {
            rs += i < aL ? a.charAt(aL - 1 - i) - '0' : 0;
            rs += i < bL ? b.charAt(bL - 1 - i) - '0' : 0;
            sb.append(rs % 2);
            rs = rs / 2;
        }
        if (rs > 0) {
            sb.append('1');
        }
        return sb.reverse().toString();
    }
}